package com.yjj.im.Utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/6/25.
 */

public class Utils {
public static Context context;


    public static void toast(String info) {
        Toast.makeText( context, info, Toast.LENGTH_SHORT).show();
    }

    // 获取当前版本号
    public static int getVersionCode(Context mContext) {
        int versionCode = 0;
        try {
            PackageManager packageManager = mContext.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    // 获取当前版名称
    public static String getVersionName(Context mContext) {
        String versionName = "";
        try {
            PackageManager packageManager = mContext.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
            versionName = packageInfo.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static boolean isNetworkAvalible() {
        // 获得网络状态管理器
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            // 建立网络数组
            NetworkInfo[] net_info = connectivityManager.getAllNetworkInfo();

            if (net_info != null) {
                for (int i = 0; i < net_info.length; i++) {
                    // 判断获得的网络状态是否是处于连接状态
                    if (net_info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void copyFile(String resFileName, String destFilename) {
        try {
            File fileIn = new File(resFileName);
            FileInputStream fileInputStream = null;
            fileInputStream = new FileInputStream(fileIn);

            InputStream in = fileInputStream;
            File file = new File(destFilename);
            file.delete();
            File parent = file.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            FileOutputStream fout = new FileOutputStream(file);
            int len = 0;
            byte buffer[] = new byte[1024];
            while ((len = in.read(buffer)) != -1) {
                fout.write(buffer, 0, len);
            }
            fout.flush();
            in.close();
            fout.close();
        } catch (IOException e) {
            Log.e("copyFile error", e.getMessage(), e);
        }
    }



}
